#include "AntMonster.h"

AntMonster::AntMonster(int locx, int locy, int locz,int extraBit, vector<vector<vector<Floor*>>> floor)
{
	collision = false;
	dir = NODIR;
	theFloor = floor;
	x = locx;
	y = locy;
	z = locz;
	type = ANTMONSTER;
	counter = 0;
	facing = (Direction)extraBit;
	removeable = false;

}

AntMonster::~AntMonster(void)
{
}

Direction AntMonster::getFacing(){return facing;}


void AntMonster::doTimeStep(Object *tait)
{
	counter++;
	int x2 = tait->getX();
	int y2 = tait->getY();
	int z2 = tait->getZ();
	int x1 = x;
	int y1 = y;
	int z1 = z;


	//
	//This next section basically ensures the ball will always go to its left if possible
	//its done by going through possibilities and seeing if hte space is free, or if it will hit tait
	//

	if(counter ==2){
		counter = 0;

		if(facing==UP){
			//CHECK COLLIDE WITH TAIT
			if(x2 == x1-1 && y2 == y1 && z2 == z1 &&isLegal(LEFT)){
				dir = LEFT;
				collision=true;
				return;
			}
			//CHECK IF LEGAL TO MOVE TO NEXT SPOT
			if(!isLegal(LEFT)||checkCollide(LEFT)){
				//CHECK COLLIDE WITH TAIT
				if(x2 == x1 && y2 == y1-1 && z2 == z1&&isLegal(UP)){
					dir = UP;
					collision=true;
					return;
				}
				//CHECK IF LEGAL TO MOVE TO NEXT SPOT
				if(!isLegal(UP)||checkCollide(UP)){
					//CHECK IF COLLIDE WITH TAIT
					if(x2 == x1+1 && y2 == y1 && z2 == z1&&isLegal(RIGHT)){
						dir = RIGHT;
						collision=true;
						return;
					}
					//CHECK IF LEGAL TO MOVE TO NEXT SPOT
					if(!isLegal(RIGHT)||checkCollide(RIGHT)){
						//CHECK IF COLLIDE WITH TAIT
						if(x2 == x1 && y2 == y1+1 && z2 == z1&&isLegal(DOWN)){
							dir = DOWN;
							collision=true;
							return;
						}
						if(!isLegal(DOWN)||checkCollide(DOWN)){
						}
						else{
							move(DOWN);
							facing=DOWN;
						}
					}

					else{
						move(RIGHT);
						facing = RIGHT;
					}
				}
				else{
					move(UP);
					facing = UP;
				}
			}
			else{
				move(LEFT);
				facing = LEFT;
			}
		}


		else if(facing==DOWN){
			//CHECK COLLIDE WITH TAIT
			if(x2 == x1+1 && y2 == y1 && z2 == z1&&isLegal(RIGHT)){
				dir = RIGHT;
				collision=true;
				return;
			}
			//CHECK IF LEGAL TO MOVE TO NEXT SPOT
			if(!isLegal(RIGHT)||checkCollide(RIGHT)){
				//CHECK COLLIDE WITH TAIT
				if(x2 == x1 && y2 == y1+1 && z2 == z1&&isLegal(DOWN)){
					dir = DOWN;
					collision=true;
					return;
				}
				//CHECK IF LEGAL TO MOVE TO NEXT SPOT
				if(!isLegal(DOWN)||checkCollide(DOWN)){
					//CHECK IF COLLIDE WITH TAIT
					if(x2 == x1-1 && y2 == y1 && z2 == z1&&isLegal(LEFT)){
						dir = LEFT;
						collision=true;
						return;
					}
					//CHECK IF LEGAL TO MOVE TO NEXT SPOT
					if(!isLegal(LEFT)||checkCollide(LEFT)){
						//CHECK IF COLLIDE WITH TAIT
						if(x2 == x1 && y2 == y1-1 && z2 == z1&&isLegal(UP)){
							dir = UP;
							collision=true;
							return;
						}
						if(!isLegal(UP)||checkCollide(UP)){
						}
						else{
							move(UP);
							facing = UP;
						}
					}

					else{
						move(LEFT);
						facing = LEFT;
					}
				}
				else{
					move(DOWN);
					facing = DOWN;
				}
			}

			else{
				move(RIGHT);
				facing = RIGHT;
			}
		}


		else if(facing==RIGHT){
			//CHECK COLLIDE WITH TAIT
			if(x2 == x1 && y2 == y1-1 && z2 == z1&&isLegal(UP)){
				dir = UP;
				collision=true;
				return;
			}
			//CHECK IF LEGAL TO MOVE TO NEXT SPOT
			if(!isLegal(UP)||checkCollide(UP)){
				//CHECK COLLIDE WITH TAIT
				if(x2 == x1+1 && y2 == y1 && z2 == z1&&isLegal(RIGHT)){
					dir = RIGHT;
					collision=true;
					return;
				}
				//CHECK IF LEGAL TO MOVE TO NEXT SPOT
				if(!isLegal(RIGHT)||checkCollide(RIGHT)){
					//CHECK IF COLLIDE WITH TAIT
					if(x2 == x1 && y2 == y1+1 && z2 == z1&&isLegal(DOWN)){
						dir = DOWN;
						collision=true;
						return;
					}
					//CHECK IF LEGAL TO MOVE TO NEXT SPOT
					if(!isLegal(DOWN)||checkCollide(DOWN)){
						//CHECK IF COLLIDE WITH TAIT
						if(x2 == x1-1 && y2 == y1 && z2 == z1&&isLegal(LEFT)){
							dir = LEFT;
							collision=true;
							return;
						}
						if(!isLegal(LEFT)||checkCollide(LEFT)){
						}
						else{
							move(LEFT);
							facing = LEFT;
						}
					}

					else{
						move(DOWN);
						facing = DOWN;
					}
				}
				else{
					move(RIGHT);
					facing =RIGHT;
				}
			}
			else{
				move(UP);
				facing = UP;
			}

		}



			else if(facing==LEFT){
				//CHECK COLLIDE WITH TAIT
				if(x2 == x1 && y2 == y1+1 && z2 == z1&&isLegal(DOWN)){
					dir = DOWN;
					collision=true;
					return;
				}
				//CHECK IF LEGAL TO MOVE TO NEXT SPOT
				if(!isLegal(DOWN)||checkCollide(DOWN)){
					//CHECK COLLIDE WITH TAIT
					if(x2 == x1-1 && y2 == y1 && z2 == z1&&isLegal(LEFT)){
						dir = LEFT;
						collision=true;
						return;
					}
					//CHECK IF LEGAL TO MOVE TO NEXT SPOT
					if(!isLegal(LEFT)||checkCollide(LEFT)){
						//CHECK IF COLLIDE WITH TAIT
						if(x2 == x1 && y2 == y1-1 && z2 == z1&&isLegal(UP)){
							dir = UP;
							collision=true;
							return;
						}
						//CHECK IF LEGAL TO MOVE TO NEXT SPOT
						if(!isLegal(UP)||checkCollide(UP)){
							//CHECK IF COLLIDE WITH TAIT
							if(x2 == x1+1 && y2 == y1 && z2 == z1&&isLegal(RIGHT)){
								dir = RIGHT;
								collision=true;
								return;
							}
							if(!isLegal(RIGHT)||checkCollide(RIGHT)){
							}
							else{
								move(RIGHT);
								facing = RIGHT;
							}
						}

						else{
							move(UP);
							facing = UP;
						}
					}
					else{
						move(LEFT);
						facing =LEFT;
					}
				}
				else{
					move(DOWN);
					facing = DOWN;
				}
			}
		}


		/*
		counter = 0;
		if(facing==UP){

		if(x2 == x1-1 && y2 == y1 && z2 == z1){
		dir = LEFT;
		collision=true;
		return;
		}
		move(LEFT);
		facing =LEFT;
		if(collision){
		if(x2 == x1 && y2 == y1-1 && z2 == z1){
		dir = UP;
		collision=true;
		return;
		}
		collision = false;
		move(UP);
		facing = UP;
		if(collision){
		if(x2 == x1+1 && y2 == y1 && z2 == z1){
		dir = RIGHT;
		collision=true;
		return;
		}
		collision = false;
		move(RIGHT);
		facing = RIGHT;

		if(collision){
		collision = false;
		move(DOWN);
		facing = DOWN;
		}
		}
		}
		}



		else if(facing==DOWN){
		if(x2 == x1+1 && y2 == y1 && z2 == z1){
		dir = RIGHT;
		collision=true;
		return;
		}
		move(RIGHT);
		facing =RIGHT;
		if(collision){
		if(x2 == x1 && y2 == y1+1 && z2 == z1){
		dir = DOWN;
		collision=true;
		return;
		}
		collision = false;
		move(DOWN);
		facing = DOWN;
		if(collision){
		if(x2 == x1-1 && y2 == y1 && z2 == z1){
		dir = LEFT;
		collision=true;
		return;
		}
		collision = false;
		move(LEFT);
		facing = LEFT;

		if(collision){
		collision = false;
		move(UP);
		facing = UP;
		}
		}
		}
		}

		else if(facing==RIGHT){
		if(x2 == x1 && y2 == y1-1 && z2 == z1){
		dir = UP;
		collision=true;
		return;
		}
		move(UP);
		facing =UP;
		if(collision){
		if(x2 == x1+1 && y2 == y1 && z2 == z1){
		dir = RIGHT;
		collision=true;
		return;
		}
		collision = false;
		move(RIGHT);
		facing = RIGHT;
		if(collision){
		if(x2 == x1 && y2 == y1+1 && z2 == z1){
		dir = DOWN;
		collision=true;
		return;
		}
		collision = false;
		move(DOWN);
		facing = DOWN;

		if(collision){
		collision = false;
		move(LEFT);
		facing = LEFT;
		}
		}
		}
		}


		else if(facing==LEFT){
		if(x2 == x1 && y2 == y1+1 && z2 == z1){
		dir = DOWN;
		collision=true;
		return;
		}
		move(DOWN);
		facing =DOWN;
		if(collision){
		if(x2 == x1-1 && y2 == y1 && z2 == z1){
		dir = LEFT;
		collision=true;
		return;
		}
		collision = false;
		move(LEFT);
		facing = LEFT;
		if(collision){
		if(x2 == x1 && y2 == y1-1 && z2 == z1){
		dir = UP;
		collision=true;
		return;
		}
		collision = false;
		move(UP);
		facing = UP;

		if(collision){
		collision = false;
		move(RIGHT);
		facing = RIGHT;
		}
		}
		}
		}


		*/

	
}

//When an this object is hit by the collider do certain stuff
void AntMonster::collide(Object* collider)
{
	//assume that collision will be over (if its not it'll flag itself again)
	collision=false;


}

//When this object hits another object do certain stuff (ie if its an exit win the game)
void AntMonster::collideWith(Object* collidee){
	//All collisions must end by flagging collision as false
	collision = false;


}

bool AntMonster::legalFloor(){
	FloorType nextFloor;
	FloorState floorState;
	FloorType currentFloor = theFloor[x][y][z]->getType();
	if(bounds()){
		switch(dir){
		case UP:	
			nextFloor = theFloor[x][y-1][z]->getType();
			floorState = theFloor[x][y-1][z]->getFloorState();
			return  !(nextFloor == WSOUTH) && !(currentFloor == WNORTH) &&!(floorState == BLOCKCREATED) 
				&& !(nextFloor == GRAVEL) && !(nextFloor==EMPTY);
			break;
		case DOWN:	
			nextFloor = theFloor[x][y+1][z]->getType();
			floorState = theFloor[x][y+1][z]->getFloorState();
			return  !(nextFloor == WNORTH)&& !(currentFloor == WSOUTH)&&!(floorState == BLOCKCREATED)
				&& !(nextFloor == GRAVEL) && !(nextFloor == WATER)&& !(nextFloor==EMPTY);
			break;
		case RIGHT:	
			nextFloor = theFloor[x+1][y][z]->getType();
			floorState = theFloor[x+1][y][z]->getFloorState();
			return  !(nextFloor == WWEST)&& !(currentFloor == WEAST)&&!(floorState == BLOCKCREATED)
				&& !(nextFloor == GRAVEL) && !(nextFloor == WATER)&& !(nextFloor==EMPTY);
			break;
		case LEFT:	
			nextFloor = theFloor[x-1][y][z]->getType();
			floorState = theFloor[x-1][y][z]->getFloorState();
			return  !(nextFloor == WEAST)&& !(currentFloor == WWEST)&&!(floorState == BLOCKCREATED)
				&& !(nextFloor == GRAVEL) && !(nextFloor == WATER) && !(nextFloor==EMPTY);
			break;

		}
	}
	return false;
}

void AntMonster::doFloor(Equip currentEquip){

	condition = FINE;
	//sliding will set you into sliding unless you're against a wall

}